const api = require('../config/api/index');
const _request = require('../common/lib/request/index');
module.exports = {
    api: api,
    ajax: _request.ajax,
    sleep: (time: any) => {
        return new Promise((resolve) => {
            setTimeout(function () {
                resolve(true);
            }, time);
        });
    },
    find({model, where}: any) {
        return new Promise(async (resolve) => {
            if (!where.deleteAt) {
                where.deleteAt = null;
            }
            const res = await this.model(model).where(where).find();
            resolve(res);
        });

    },
    select({model,where,field,join}: any) {
        return new Promise(async (resolve) => {
            if(!where){
                where = {1:1};
            }
            if (!where.deleteAt) {
                where.deleteAt = null;
            }
            let _model = this.model(model);
            if(join){
                _model = _model.join(join)
            }
            if(field){
                _model = _model.field(field)
            }

            const res = await _model.where(where).select();
            resolve(res);
        });

    },
    addMany({model, where,data}: any) {
        return new Promise(async (resolve) => {
            const res = await this.model(model).addMany(data);
            resolve(res);
        });

    },
    updateMany({model, where,data}: any) {
        return new Promise(async (resolve) => {
            const res = await this.model(model).updateMany(data);
            resolve(res);
        });
    },
    update({model, data, where}: any) {
        return new Promise(async (resolve) => {
            const updatedAt = this.formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
            if (!data.updatedAt) {
                data.updatedAt = updatedAt;
            }
            if (!where.deleteAt) {
                where.deleteAt = null;
            }
            const res = await this.model(model).where(where).update(data);
            resolve(res);
        });

    },
    softDelete({model, where}: any) {
        return new Promise(async (resolve) => {
            const deleteAt = this.formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');
            const res = await this.model(model).where(where).update({deleteAt});
            resolve(res);
        });

    },
    formatDate: (timestampOrDate: any, fmt: string) => {
        if (!timestampOrDate) {
            return '';
        }
        let date = null;
        if (typeof timestampOrDate === 'object') {
            date = timestampOrDate;
        }
        else if (!/^\d+$/.test(timestampOrDate)) {
            date = new Date(timestampOrDate.replace(/-/gi, '/'));
        }
        else {
            date = new Date(timestampOrDate);
        }
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'H+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds(),
            'q+': Math.floor((date.getMonth() + 3) / 3),
            S: date.getMilliseconds()
        };
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(o[k].toString().length));
            }
        }
        return fmt;
    },
};
